package com.enlighten.featureExtractor.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtils {

	public static BufferedImage getCompatibleImage(BufferedImage image) {
		int height = image.getHeight();
		int width = image.getWidth();
		int newHeight = height % 2 == 0 ? height+1 : height;
		int newWidth = width % 2 == 0 ? width+1 : width;
		
		if ( newHeight > newWidth )
			newWidth = newHeight;
		else
			newHeight = newWidth;
		
		BufferedImage bufferedImage = new BufferedImage(newWidth, newHeight, image.getType());
		Graphics2D graphics = bufferedImage.createGraphics();
		graphics.setColor(new Color(image.getRGB(0, 0)));
		graphics.fillRect(width, 0, newWidth-width, newHeight);
		graphics.fillRect(0, height, newWidth, newHeight-height);
		graphics.drawImage(image,null,0,0);
		graphics.dispose();
		try {
			ImageIO.write(bufferedImage, "PNG", new File("example.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bufferedImage;
	}

}
